1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.collect.CollectPreconditions.checkNonnegative;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.annotations.VisibleForTesting;
24
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @GwtCompatible(serializable = true, emulated = true)
67 public final class ArrayListMultimap<K, V> extends AbstractListMultimap<K, V> {
68
69 private static final int DEFAULT_VALUES_PER_KEY = 3;
70
71 @VisibleForTesting transient int expectedValuesPerKey;
72
73
74
75
76
77 public static <K, V> ArrayListMultimap<K, V> create() {
78 return new ArrayListMultimap<K, V>();
79 }
80
81
82
83
84
85
86
87
88
89
90 public static <K, V> ArrayListMultimap<K, V> create(
91 int expectedKeys, int expectedValuesPerKey) {
92 return new ArrayListMultimap<K, V>(expectedKeys, expectedValuesPerKey);
93 }
94
95
96
97
98
99
100
101 public static <K, V> ArrayListMultimap<K, V> create(
102 Multimap<? extends K, ? extends V> multimap) {
103 return new ArrayListMultimap<K, V>(multimap);
104 }
105
106 private ArrayListMultimap() {
107 super(new HashMap<K, Collection<V>>());
108 expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
109 }
110
111 private ArrayListMultimap(int expectedKeys, int expectedValuesPerKey) {
112 super(Maps.<K, Collection<V>>newHashMapWithExpectedSize(expectedKeys));
113 checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
114 this.expectedValuesPerKey = expectedValuesPerKey;
115 }
116
117 private ArrayListMultimap(Multimap<? extends K, ? extends V> multimap) {
118 this(multimap.keySet().size(),
119 (multimap instanceof ArrayListMultimap) ?
120 ((ArrayListMultimap<?, ?>) multimap).expectedValuesPerKey :
121 DEFAULT_VALUES_PER_KEY);
122 putAll(multimap);
123 }
124
125
126
127
128
129 @Override List<V> createCollection() {
130 return new ArrayList<V>(expectedValuesPerKey);
131 }
132
133
134
135
136 public void trimToSize() {
137 for (Collection<V> collection : backingMap().values()) {
138 ArrayList<V> arrayList = (ArrayList<V>) collection;
139 arrayList.trimToSize();
140 }
141 }
142
143
144
145
146
147
148 @GwtIncompatible("java.io.ObjectOutputStream")
149 private void writeObject(ObjectOutputStream stream) throws IOException {
150 stream.defaultWriteObject();
151 stream.writeInt(expectedValuesPerKey);
152 Serialization.writeMultimap(this, stream);
153 }
154
155 @GwtIncompatible("java.io.ObjectOutputStream")
156 private void readObject(ObjectInputStream stream)
157 throws IOException, ClassNotFoundException {
158 stream.defaultReadObject();
159 expectedValuesPerKey = stream.readInt();
160 int distinctKeys = Serialization.readCount(stream);
161 Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
162 setMap(map);
163 Serialization.populateMultimap(this, stream, distinctKeys);
164 }
165
166 @GwtIncompatible("Not needed in emulated source.")
167 private static final long serialVersionUID = 0;
168 }